package com.teamcenter.rac.massupdate.ui;

import java.util.Iterator;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.IPropertySource;

import com.teamcenter.rac.common.TCUtilities;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.massupdate.converters.Source2LabelConverter;
import com.teamcenter.rac.massupdate.model.MarkupChangeModel;
import com.teamcenter.rac.massupdate.model.MarkupModel;
import com.teamcenter.rac.util.AdapterUtil;
import com.teamcenter.rac.viewer.listener.ColumnListener;
import com.teamcenter.rac.viewer.view.ViewerHelper;
@SuppressWarnings({"rawtypes"})
public class MarkupResultsDialog
  extends AbstractMassUpdateDialog
{
  protected Button m_manageMarkupButton;
  private Table m_markupChangesTable;
  protected MarkupModel m_markupModel;
  private Text m_markupObjectCommentText;
  private Text m_markupObjectNameText;
  private Text m_markupObjectPurposeText;
  private TableViewer m_tableViewer;
  
  public MarkupResultsDialog(Shell paramShell, TCComponent paramTCComponent, List<TCComponent> paramList)
  {
    super(paramShell);
    setShellStyle(36080);
    if ((paramTCComponent != null) && (paramTCComponent.getType().equals("Fnd0Markup")))
    {
      IPropertySource localIPropertySource1 = (IPropertySource)AdapterUtil.getAdapter(paramTCComponent, IPropertySource.class);
      this.m_markupModel = getMarkupModel(localIPropertySource1);
      if (paramList != null)
      {
        Iterator localIterator = paramList.iterator();
        while (localIterator.hasNext())
        {
          TCComponent localTCComponent = (TCComponent)localIterator.next();
          IPropertySource localIPropertySource2 = (IPropertySource)AdapterUtil.getAdapter(localTCComponent, IPropertySource.class);
          if (localIPropertySource2 != null) {
            this.m_markupModel.addMarkupChange(new MarkupChangeModel(localIPropertySource2));
          }
        }
      }
    }
  }
  
  protected MarkupModel getMarkupModel(IPropertySource paramIPropertySource)
  {
    if (this.m_markupModel == null) {
      return new MarkupModel(paramIPropertySource);
    }
    return this.m_markupModel;
  }
  
  protected void createButtonsForButtonBar(Composite paramComposite)
  {
    this.m_manageMarkupButton = createButton(paramComposite, 1060, Messages.MassUpdateDialog_Btn_ManageMarkup, false);
    this.m_manageMarkupButton.setToolTipText(Messages.MarkupResultsDialog_manageMarkupButton_toolTipText);
    this.m_manageMarkupButton.addSelectionListener(new SelectionAdapter()
    {
      public void widgetSelected(SelectionEvent paramAnonymousSelectionEvent)
      {
        MarkupResultsDialog.this.setReturnCode(1060);
        MarkupResultsDialog.this.close();
      }
    });
    Button localButton = createButton(paramComposite, 12, IDialogConstants.CLOSE_LABEL, false);
    localButton.addSelectionListener(new SelectionAdapter()
    {
      public void widgetSelected(SelectionEvent paramAnonymousSelectionEvent)
      {
        MarkupResultsDialog.this.close();
      }
    });
    localButton.setFocus();
    this.m_bindingContext = initDataBindings();
    setupAggregateValidationStatus();
    initCommonDataBindings(this.m_bindingContext);
  }
  
  protected Control createContents(Composite paramComposite)
  {
    Control localControl = super.createContents(paramComposite);
    setTitle(Messages.UpdateResultsDialog_Title);
    getShell().setText(Messages.UpdateResultsDialog_Title);
    setMessage("");
    return localControl;
  }
  
  protected Control createDialogArea(Composite paramComposite)
  {
    Composite localComposite1 = (Composite)super.createDialogArea(paramComposite);
    GridLayout localGridLayout = new GridLayout(1, false);
    localGridLayout.verticalSpacing = 0;
    localGridLayout.marginWidth = 0;
    localGridLayout.marginHeight = 0;
    localGridLayout.horizontalSpacing = 0;
    Composite localComposite2 = new Composite(localComposite1, 0);
    localComposite2.setLayoutData(new GridData(4, 4, true, true, 1, 1));
    localComposite2.setLayout(localGridLayout);
    contributeChangeInformationGroup(localComposite2);
    contributeChangeTableGroup(localComposite2);
    return localComposite1;
  }
  
  protected void initCommonDataBindings(DataBindingContext paramDataBindingContext) {}
  
  protected DataBindingContext initDataBindings()
  {
    DataBindingContext localDataBindingContext = new DataBindingContext();
    ISWTObservableValue localISWTObservableValue1 = SWTObservables.observeText(this.m_markupObjectNameText, 24);
    IObservableValue localIObservableValue1 = BeansObservables.observeValue(this.m_markupModel, "markup");
    UpdateValueStrategy localUpdateValueStrategy = new UpdateValueStrategy();
    localUpdateValueStrategy.setConverter(new Source2LabelConverter());
    localDataBindingContext.bindValue(localISWTObservableValue1, localIObservableValue1, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), localUpdateValueStrategy);
    ISWTObservableValue localISWTObservableValue2 = SWTObservables.observeText(this.m_markupObjectPurposeText, 24);
    IObservableValue localIObservableValue2 = BeansObservables.observeValue(this.m_markupModel, "fnd0purpose");
    localDataBindingContext.bindValue(localISWTObservableValue2, localIObservableValue2, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
    ISWTObservableValue localISWTObservableValue3 = SWTObservables.observeText(this.m_markupObjectCommentText, 24);
    IObservableValue localIObservableValue3 = BeansObservables.observeValue(this.m_markupModel, "fnd0comment");
    localDataBindingContext.bindValue(localISWTObservableValue3, localIObservableValue3, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
    return localDataBindingContext;
  }
  
  private void contributeActualTableColumns(TableViewer paramTableViewer)
  {
    String[] arrayOfString1 = { "fnd0modified_object", "fnd0context", "fnd0markup_type", "fnd0input_object", "fnd0status", "fnd0note" };
    int[] arrayOfInt = { 120, 120, 120, 120, 120, 120 };
    String[] arrayOfString2 = TCUtilities.getLocalizedValues(MarkupChangeModel.MARKUP_CHANGE_PROP_NAMES);
    arrayOfString2[2] = Messages.MarkupChange_PropDisplayName_MarkupType;
    ViewerHelper.addViewerColumns(paramTableViewer, arrayOfString1, arrayOfString2, null, false);
    int i = this.m_markupChangesTable.getColumnCount();
    for (int j = 0; j < i; j++)
    {
      TableColumn localTableColumn = this.m_markupChangesTable.getColumn(j);
      localTableColumn.setMoveable(true);
      localTableColumn.setResizable(true);
      localTableColumn.setWidth(arrayOfInt[j]);
      ColumnListener localColumnListener = new ColumnListener(paramTableViewer);
      localTableColumn.addSelectionListener(localColumnListener);
      localTableColumn.addControlListener(localColumnListener);
    }
  }
  
  private void contributeChangeInformationGroup(Composite paramComposite)
  {
    GridLayout localGridLayout1 = new GridLayout(1, false);
    localGridLayout1.verticalSpacing = 0;
    localGridLayout1.marginWidth = 0;
    localGridLayout1.marginHeight = 0;
    localGridLayout1.horizontalSpacing = 0;
    Group localGroup = new Group(paramComposite, 0);
    localGroup.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
    localGroup.setLayout(localGridLayout1);
    localGroup.setText(Messages.ChangeMarkupDialog_Grp_ChangeInformation);
    GridLayout localGridLayout2 = new GridLayout(2, false);
    localGridLayout2.marginLeft = 10;
    Composite localComposite = new Composite(localGroup, 0);
    localComposite.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
    localComposite.setLayout(localGridLayout2);
    Label localLabel1 = new Label(localComposite, 0);
    FontData localFontData1 = localLabel1.getFont().getFontData()[0];
    localLabel1.setFont(JFaceResources.getFontRegistry().getBold(localFontData1.getName()));
    localLabel1.setLayoutData(new GridData(131072, 16777216, false, false, 1, 1));
    localLabel1.setText(Messages.ChangeMarkupDialog_Lbl_MarkupObject);
    this.m_markupObjectNameText = new Text(localComposite, 8);
    this.m_markupObjectNameText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
    Label localLabel2 = new Label(localComposite, 0);
    FontData localFontData2 = localLabel2.getFont().getFontData()[0];
    localLabel2.setFont(JFaceResources.getFontRegistry().getBold(localFontData2.getName()));
    localLabel2.setText(Messages.ChangeMarkupDialog_Lbl_Purpose);
    this.m_markupObjectPurposeText = new Text(localComposite, 8);
    this.m_markupObjectPurposeText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
    Label localLabel3 = new Label(localComposite, 0);
    FontData localFontData3 = localLabel3.getFont().getFontData()[0];
    localLabel3.setFont(JFaceResources.getFontRegistry().getBold(localFontData3.getName()));
    localLabel3.setText(Messages.ChangeMarkupDialog_Lbl_Comment);
    this.m_markupObjectCommentText = new Text(localComposite, 8);
    this.m_markupObjectCommentText.setLayoutData(new GridData(4, 16777216, true, false, 1, 1));
  }
  
  protected void contributeChangeTableGroup(Composite paramComposite)
  {
    Group localGroup = new Group(paramComposite, 0);
    localGroup.setLayoutData(new GridData(4, 4, true, true, 1, 1));
    localGroup.setText(Messages.MassUpdateDialog_Grp_OperationalData);
    GridLayout localGridLayout1 = new GridLayout(1, false);
    localGridLayout1.verticalSpacing = 0;
    localGridLayout1.marginWidth = 0;
    localGridLayout1.marginHeight = 0;
    localGridLayout1.horizontalSpacing = 0;
    localGroup.setLayout(localGridLayout1);
    Composite localComposite = new Composite(localGroup, 0);
    localComposite.setLayoutData(new GridData(4, 4, true, true, 1, 1));
    GridLayout localGridLayout2 = new GridLayout(1, false);
    localGridLayout2.verticalSpacing = 0;
    localGridLayout2.marginWidth = 0;
    localGridLayout2.marginHeight = 0;
    localGridLayout2.horizontalSpacing = 0;
    localComposite.setLayout(localGridLayout2);
    this.m_tableViewer = new TableViewer(localComposite, 68352);
    this.m_tableViewer.setComparator(new AbstractMassUpdateDialog.IC_PropertyColumnViewerSorter());
    this.m_markupChangesTable = this.m_tableViewer.getTable();
    this.m_markupChangesTable.setLinesVisible(true);
    this.m_markupChangesTable.setHeaderVisible(true);
    this.m_markupChangesTable.setLayoutData(new GridData(4, 4, true, true, 1, 1));
    contributeActualTableColumns(this.m_tableViewer);
    this.m_tableViewer.setContentProvider(new ObservableListContentProvider());
    this.m_tableViewer.setInput(this.m_markupModel.getMarkupChanges());
  }
}
